package com.df.bpm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.df.bpm.entity.BusinessInputs;
import com.df.bpm.entity.BusinessLocations;
import com.df.bpm.entity.BusinessOutputs;
import com.df.common.service.impl.BaseServiceImpl;
import com.df.bpm.entity.BusinessInformation;
import com.df.bpm.mapper.BusinessInformationMapper;
import com.df.bpm.service.BusinessInformationService;

import com.df.common.utils.CommonUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class BusinessInformationServiceImpl extends BaseServiceImpl<BusinessInformationMapper,BusinessInformation> implements BusinessInformationService {
	public final BusinessInformationMapper businessInformationMapper;

	/**
	 * 业务定位服务
	 */
	public final BusinessLocationsServiceImpl businessLocationsServiceImpl;

	private final BusinessOutputsServiceImpl businessOutputsServiceImpl;

	private final BusinessInputsServiceImpl businessInputsServiceImpl;

	/**
	 * 深度删除
	 * <P>递归删除所有子级业务</P>
	 * @param businessInformationIndex 目标业务ID
	 */
	public Integer deleteDeeply(Integer businessInformationIndex){
		int result=0;
		if(businessInformationIndex==null){
			return result;
		}
		List<BusinessLocations> locations=businessLocationsServiceImpl.getList(new BusinessLocations().setBusinessInformationIndex(businessInformationIndex));
		if(CommonUtils.varIsNotBlank(locations)){//删除本业务所有位置
			for(BusinessLocations location:locations){
				businessLocationsServiceImpl.updateSequences4delete(location.getSequence(), location.getSuperior(), location.getGroupID());
			}
			QueryWrapper<BusinessLocations> wrapper=new QueryWrapper<>();
			wrapper.eq("businessInformationIndex", businessInformationIndex);
			result+=businessLocationsServiceImpl.delete(wrapper);
		}
		result+=businessInformationMapper.deleteById(businessInformationIndex);
		List<BusinessLocations> sons=businessLocationsServiceImpl.getList(new BusinessLocations().setSuperior(businessInformationIndex));
		if(CommonUtils.varIsNotBlank(sons)){//删除所有子级业务
			for(BusinessLocations son:sons){
				result+=deleteDeeply(son.getBusinessInformationIndex());
			}
		}
		return result;
	}


	/**
	 * 刷新业务输出物输入物
	 * <P>从子级过滤中间件后整理添加（不删除）<P/>
	 * @param businessInformationIndex 要刷新的业务id
	 * @return
	 */
    public void freshBusinessEntities(Integer businessInformationIndex) {
		List<Integer> outputsId4Son=businessInformationMapper.getSonsOutputs(businessInformationIndex);
		List<Integer> inputsId4Son=businessInformationMapper.getSonsInputs(businessInformationIndex);
		List<Integer> outputsId=businessInformationMapper.getOutputs(businessInformationIndex);
		List<Integer> inputsId=businessInformationMapper.getInputs(businessInformationIndex);
		for(Integer id:outputsId4Son){
			if(!inputsId4Son.contains(id) && !outputsId.contains(id)){
				BusinessOutputs temp=new BusinessOutputs();
				temp.setBusinessInformationIndex(businessInformationIndex);
				temp.setBusinessEntityIndex(id);
				businessOutputsServiceImpl.add(temp);
			}
		}
		for(Integer id:inputsId4Son){
			if(!outputsId4Son.contains(id) && !inputsId.contains(id)){
				BusinessInputs temp=new BusinessInputs();
				temp.setBusinessInformationIndex(businessInformationIndex);
				temp.setBusinessEntityIndex(id);
				businessInputsServiceImpl.add(temp);
			}
		}
    }
}